//
//  ViewController.m
//  ZLPhotoBrowser
//
//  Created by long on 15/12/1.
//  Copyright © 2015年 long. All rights reserved.
//

#import "ViewController.h"
#import "ZLPhotoActionSheet.h"
#import "FSProgressPhotosCell.h"
#import "FSDecorateMotailViewController.h"

#define cellIdentifier @"cellIdentifier"
//能选多少张
#define MAX_PHOTOS          9
//平铺多少张
#define MAX_PLAN_PHOTOS     7

//end

///////////////////////////////////////////////////
// git 地址： https://github.com/longitachi/ZLPhotoBrowser
// 喜欢的朋友请去给个star，莫大的支持，谢谢
///////////////////////////////////////////////////
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    ZLPhotoActionSheet *actionSheet;
    
}
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray  *imgData;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _imgData = [NSMutableArray array];
    
    [self initWithActionSheetPhoto];
    //初始化布局
    [self initWithCollectionView];
}

- (IBAction)btnSelectPhoto_Click:(id)sender
{
    __weak typeof(self) weakSelf = self;
    [actionSheet showWithSender:self animate:YES completion:^(NSArray<UIImage *> * _Nonnull selectPhotos) {
        
        [weakSelf reloadView:(NSMutableArray*)selectPhotos isAddPhoto:YES];
        
        
    }];
}

- (void)initWithActionSheetPhoto
{
    // Do any additional setup after loading the view, typically from a nib.
    actionSheet = [[ZLPhotoActionSheet alloc] init];
    //设置照片最大选择数
    actionSheet.maxSelectCount = MAX_PHOTOS;
    //设置照片最大预览数
    actionSheet.maxPreviewCount = 20;
}

- (void)initWithCollectionView
{
    //平均宽度(放四张)
    CGFloat width = (SCREEN_WIDTH_BOUNDS-space*2)/MAX_PLAN_PHOTOS-space;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(width, width);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(space, 64+space*6, SCREEN_WIDTH_BOUNDS-space*2, width*2+space) collectionViewLayout:layout];
    _collectionView.delegate =self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[FSProgressPhotosCell class] forCellWithReuseIdentifier:cellIdentifier];
    
    [self.view addSubview:_collectionView];
}

#pragma mrak   collectionView 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imgData.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FSProgressPhotosCell *cell = (FSProgressPhotosCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell cellDataWithArray:_imgData position:indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    // 图片浏览
    FSDecorateMotailViewController *motailVc = [[FSDecorateMotailViewController alloc] init];
    motailVc.isShowDeleBt = YES;
    //图片数组
    motailVc.dataArr = _imgData;
    //选中图片
    motailVc.seletTag = indexPath.row;
    __weak typeof(self)weakSelf = self;
    //返回的回调
    motailVc.callBackBlock = ^(NSMutableArray *array){
        [weakSelf reloadView:array isAddPhoto:NO];
    };
    [self.navigationController pushViewController:motailVc animated:YES];
    
}

- (void)reloadView:(NSMutableArray*)imgData isAddPhoto:(BOOL)isAddPhoto
{
    
    NSInteger maxPhotos = 0;
    if (isAddPhoto == YES) {
        //重置图片数组
        [self.imgData addObjectsFromArray:imgData];
    }else{
        self.imgData = imgData;
    }
    //计算还能选几张
    if (self.imgData.count > 0) {
    maxPhotos = MAX_PHOTOS-self.imgData.count>0?MAX_PHOTOS-self.imgData.count:0;
    }else{
        maxPhotos = MAX_PHOTOS-imgData.count>0?MAX_PHOTOS-imgData.count:0;
    }
    
    actionSheet.maxSelectCount = maxPhotos;
    
    //刷新数据
    [self.collectionView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com